package com.chinatechstar.account.controller;

import java.util.LinkedHashMap;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.chinatechstar.account.entity.SysUserDetail;
import com.chinatechstar.account.service.SysUserDetailService;
import com.chinatechstar.component.commons.client.ClientResponse;
import com.chinatechstar.component.commons.client.ResultCode;
import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.validator.InsertValidator;
import com.chinatechstar.component.commons.validator.UpdateDetailValidator;

/**
 * 用户详细信息的控制层
 */
@Api(value = "用户详细信息的控制层", tags = {"用户详细信息"})
@RestController
@RequestMapping("/sysuserdetail")
public class SysUserDetailController {

    @Autowired
    private SysUserDetailService sysUserDetailService;

    /**
     * 查询当前用户的授权角色、菜单和按钮
     *
     * @param username 用户名
     * @param mobile   手机号
     * @return
     */
    @ApiOperation(value = "查询当前用户的授权角色、菜单和按钮", tags = {"用户详细信息"})
    @PostMapping(path = "/queryCurrentAuthority")
    public LinkedHashMap<String, Object> queryCurrentAuthority(@RequestParam(name = "username", required = false) String username,
                                                               @RequestParam(name = "mobile", required = false) String mobile) {
        if (StringUtils.isBlank(username) && StringUtils.isBlank(mobile)) {
            throw new IllegalArgumentException("用户名或手机号不能为空");
        }
        LinkedHashMap<String, Object> authorityMap = new LinkedHashMap<>();
        authorityMap.put("status", 200);
        authorityMap.put("type", "account");
        String roleCode = sysUserDetailService.getRoleCodeBySysUser(username, mobile);
        authorityMap.put("currentAuthority", roleCode);
        List<LinkedHashMap<String, Object>> menuList = sysUserDetailService.querySysMenuAuthorityTree(username, mobile);
        authorityMap.put("menu", menuList);
        List<String> menuButtonList = sysUserDetailService.queryRoleMenuButton(username, mobile);
        authorityMap.put("menuButton", menuButtonList);
        return authorityMap;
    }

    /**
     * 注册用户
     *
     * @param sysUserDetail 用户详细信息
     * @return
     */
    @ApiOperation(value = "注册用户", tags = {"用户详细信息"})
    @PostMapping(path = "/registerAccount")
    public ActionResult registerAccount(@Validated(InsertValidator.class) @RequestBody SysUserDetail sysUserDetail) {
        sysUserDetailService.insertSysUser(sysUserDetail);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 编辑用户详细信息
     *
     * @param sysUserDetail 用户详细信息
     * @return
     */
    @ApiOperation(value = "编辑用户详细信息", tags = {"用户详细信息"})
    @PutMapping(path = "/updateSysUserDetail")
    public ActionResult updateSysUserDetail(@Validated(UpdateDetailValidator.class) @RequestBody SysUserDetail sysUserDetail) {
        sysUserDetailService.updateSysUserDetail(sysUserDetail);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 根据字段编辑用户信息
     *
     * @param fieldValue 修改的字段值
     * @param field      修改的字段
     * @param id         用户ID
     * @return
     */
    @ApiOperation(value = "根据字段编辑用户信息", tags = {"用户详细信息"})
    @PutMapping(path = "/updateSysUserInfo")
    public ActionResult updateSysUserInfo(@RequestParam(name = "fieldValue") String fieldValue,
                                          @RequestParam(name = "field") String field, @RequestParam(name = "id") Long id) {
        sysUserDetailService.updateSysUserInfo(fieldValue, field, id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 修改用户密码
     *
     * @param password    原密码
     * @param newPassword 新密码
     * @param id          用户ID
     * @return
     */
    @ApiOperation(value = "修改用户密码", tags = {"用户详细信息"})
    @PutMapping(path = "/updatePassword")
    public ActionResult updatePassword(@RequestParam(name = "password") String password,
                                       @RequestParam(name = "newPassword") String newPassword, @RequestParam(name = "id") Long id) {
        sysUserDetailService.updatePassword(password, newPassword, id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 找回密码
     *
     * @param email       邮箱
     * @param charCaptcha 验证码字符
     * @return
     */
    @ApiOperation(value = "找回密码", tags = {"用户详细信息"})
    @PostMapping(path = "/retrievePassword")
    public ActionResult retrievePassword(@RequestParam(name = "email") String email,
                                         @RequestParam(name = "charCaptcha") String charCaptcha) {
        sysUserDetailService.retrievePassword(email, charCaptcha);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 比对验证码
     *
     * @param charCaptcha 验证码字符
     * @return
     */
    @ApiOperation(value = "比对验证码", tags = {"用户详细信息"})
    @PostMapping(path = "/compareCaptcha")
    public ActionResult compareCaptcha(@RequestParam(name = "charCaptcha") String charCaptcha) {
        sysUserDetailService.compareCaptcha(charCaptcha);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 修改用户头像图片地址
     *
     * @param avatar 用户头像图片地址
     * @param id     用户ID
     */
    @ApiOperation(value = "修改用户头像图片地址", tags = {"用户详细信息"})
    @PostMapping(value = "/updateAvatar")
    public ClientResponse updateAvatar(@RequestParam(name = "avatar") String avatar, @RequestParam(name = "id") Long id) {
        try {
            sysUserDetailService.updateSysUserInfo(avatar, "avatar", id);
            return new ClientResponse(ResultCode.SUCCESS);
        } catch (Exception e) {
            return new ClientResponse(ResultCode.FAILURE, e.toString());
        }
    }

}
